#include "config_main.h"
#ifdef DDENN_TIMERS_DOWN
/*
 * timers.c
 *
 *  Created on: 06.06.2009
 *      Author: Admin
 */
#include "timers_down.h"

/**************************************************************************************/
//
//          it provides ddenn/timers/
//
/*************************************************************************************/


DdennTimersDown::ddenn_timer DdennTimersDown::DdennTimersDown[DDENN_TIMERS_DOWN_COUNT];


void DdennTimersDown::RegisterDdennTimersDown(){
	for (int i=0; i<(DDENN_TIMERS_DOWN_COUNT); ++i){
		dden_timer_create(&(DdennTimersDown[i]));

		XPLMRegisterDataAccessor(  StPrintI("ddenn/timers_down/timer%d_hours", i),    xplmType_Int, 1,
												   GetTimerHours,
												   SetTimerHours,NULL,NULL,NULL, NULL,NULL,NULL,NULL,  NULL,NULL,    NULL,  &(DdennTimersDown[i]), &(DdennTimersDown[i]));

		XPLMRegisterDataAccessor(StPrintI("ddenn/timers_down/timer%d_minutes", i) ,    xplmType_Int, 1,
												   GetTimerMinutes,
												   SetTimerMinutes,NULL,NULL,NULL, NULL,NULL,NULL,NULL,  NULL,NULL,    NULL,  &(DdennTimersDown[i]), &(DdennTimersDown[i]));

		XPLMRegisterDataAccessor( StPrintI("ddenn/timers_down/timer%d_seconds", i)  ,    xplmType_Int, 1,
												   GetTimerSeconds,
												   SetTimerSeconds,NULL,NULL,NULL, NULL,NULL,NULL,NULL,  NULL,NULL,    NULL,  &(DdennTimersDown[i]), &(DdennTimersDown[i]));

		XPLMRegisterDataAccessor(StPrintI("ddenn/timers_down/timer%d_raw", i)  ,    xplmType_Float, 0,NULL,NULL,
													   GetTimerRaw,
													   NULL,NULL, NULL,NULL,NULL,NULL,  NULL,NULL,    NULL,  &(DdennTimersDown[i]), &(DdennTimersDown[i]));

		XPLMRegisterDataAccessor(StPrintI("ddenn/timers_down/timer%d_pause", i)  ,    xplmType_Int, 1,
													   GetTimerPause,
													   SetTimerPause,NULL,NULL,NULL, NULL,NULL,NULL,NULL,  NULL,NULL,    NULL,  &(DdennTimersDown[i]), &(DdennTimersDown[i]));

		XPLMRegisterDataAccessor(StPrintI( "ddenn/timers_down/timer%d_expired", i) ,    xplmType_Int, 0,
													   GetTimerExpired,
													   NULL,NULL,NULL,NULL, NULL,NULL,NULL,NULL,  NULL,NULL,    NULL,  &(DdennTimersDown[i]), &(DdennTimersDown[i]));
	}
}


void DdennTimersDown::UnregisterDdennTimersDown(){
	for (int i=0; i<(DDENN_TIMERS_DOWN_COUNT); ++i){
		XPLMUnregisterDataAccessor( XPLMFindDataRef(StPrintI( "ddenn/timers_down/timer%d_hours", i) ));
		XPLMUnregisterDataAccessor( XPLMFindDataRef(StPrintI(  "ddenn/timers_down/timer%d_minutes", i)));
		XPLMUnregisterDataAccessor( XPLMFindDataRef(StPrintI( "ddenn/timers_down/timer%d_seconds", i)));
		XPLMUnregisterDataAccessor( XPLMFindDataRef(StPrintI( "ddenn/timers_down/timer%d_raw", i)));
		XPLMUnregisterDataAccessor( XPLMFindDataRef(StPrintI("ddenn/timers_down/timer%d_pause", i) ));
		XPLMUnregisterDataAccessor( XPLMFindDataRef(StPrintI("ddenn/timers_down/timer%d_expired", i) ));
	}
}



void DdennTimersDown::dden_timer_create (ddenn_timer *  t)
{
	(*t).starttime = 0;
	(*t).pausetime = 0;
	(*t).pausedtime = 0;
	(*t).start_hours=0;
	(*t).start_minutes=0;
	(*t).start_seconds=0;
	(*t).ispaused = 1;

};


void DdennTimersDown:: dden_timer_set (ddenn_timer *  t)
{
	(*t).starttime = GetXPlaneLocalTime();
	(*t).pausetime = GetXPlaneLocalTime();
	(*t).pausedtime = 0;
	(*t).ispaused = 1;
};


void DdennTimersDown::ddenn_timer_pause (ddenn_timer *  t)
{
	(*t).pausetime = GetXPlaneLocalTime();
	(*t).ispaused = 1;
};


void DdennTimersDown::ddenn_timer_play (ddenn_timer *  t)
{
	(*t).ispaused = 0;
	(*t).pausedtime =(*t).pausedtime + (GetXPlaneLocalTime() - ((*t).pausetime));

};


float DdennTimersDown::ddenn_timer_gettime (ddenn_timer  t)
{
	float x=(t).start_seconds+(t).start_minutes*60+(t).start_hours*360;
	if (t.ispaused){
	  return x-((t.pausetime)-(t.pausedtime) -(t.starttime));
	}
	else{
	  return x-((GetXPlaneLocalTime()) -(t.starttime) - (t.pausedtime));
	}
};



int DdennTimersDown::GetTimerHours ( void *               inRefcon)
{
	ddenn_timer *p = (ddenn_timer*)inRefcon;
	return abs(UTCTimeToHours(ddenn_timer_gettime(*p)));
}
int DdennTimersDown::GetTimerMinutes ( void *               inRefcon)
{
	ddenn_timer *p = (ddenn_timer*)inRefcon;
	return abs(UTCTimeToMinutes(ddenn_timer_gettime(*p)));
}

int DdennTimersDown::GetTimerSeconds ( void *               inRefcon)
{
	ddenn_timer *p = (ddenn_timer*)inRefcon;
	return abs(UTCTimeToSeconds(ddenn_timer_gettime(*p)));
}




void DdennTimersDown::SetTimerHours (void *               inRefcon,
        int                  inValue)
{
	ddenn_timer *p = (ddenn_timer*)inRefcon;
	(*p).start_hours=inValue;
	dden_timer_set(p);
}
void DdennTimersDown::SetTimerMinutes (void *               inRefcon,
        int                  inValue)
{
	ddenn_timer *p = (ddenn_timer*)inRefcon;
	(*p).start_minutes=inValue;
	dden_timer_set(p);
}

void DdennTimersDown::SetTimerSeconds (void *               inRefcon,
        int                  inValue)
{
	ddenn_timer *p = (ddenn_timer*)inRefcon;
	(*p).start_seconds=inValue;
	dden_timer_set(p);
}



float DdennTimersDown::GetTimerRaw ( void *               inRefcon)
{
	ddenn_timer *p = (ddenn_timer*)inRefcon;
	return ddenn_timer_gettime(*p);
}



int DdennTimersDown::GetTimerPause ( void *               inRefcon)
{
	ddenn_timer *p = (ddenn_timer*)inRefcon;
	if ((*p).ispaused) {return 1;} else {return 0;};
}

int DdennTimersDown::GetTimerExpired ( void *               inRefcon)
{
	ddenn_timer *p = (ddenn_timer*)inRefcon;
	if (ddenn_timer_gettime(*p)<=0) {return 1;} else {return 0;};
}


void DdennTimersDown::SetTimerPause(void *               inRefcon,
                 int                  inValue)
{
	ddenn_timer *p = (ddenn_timer*)inRefcon;
	if (inValue) {
		if ((*p).ispaused){
			ddenn_timer_play((p));
		}else{
			ddenn_timer_pause((p));
		}
	};
}



/**************************************************************************************/
//
//          end of ddenn/timers/
//
/*************************************************************************************/


#endif
